{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/user/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "\n",
    "#又提到了那个快餐点餐系统，不过今天我们只以其中的一个类作为主角：饮料类。首先，回忆下饮料类：\n",
    "class Beverage():\n",
    "    name = \"\"\n",
    "    price = 0.0\n",
    "    type = \"BEVERAGE\"\n",
    "    def getPrice(self):\n",
    "        return self.price\n",
    "    def setPrice(self, price):\n",
    "        self.price = price\n",
    "    def getName(self):\n",
    "        return self.name\n",
    "\n",
    "class coke(Beverage):\n",
    "    def __init__(self):\n",
    "        self.name = \"coke\"\n",
    "        self.price = 4.0\n",
    "\n",
    "class milk(Beverage):\n",
    "    def __init__(self):\n",
    "        self.name = \"milk\"\n",
    "        self.price = 5.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#除了基本配置，快餐店卖可乐时，可以选择加冰，如果加冰的话，要在原价上加0.3元；\n",
    "# 卖牛奶时，可以选择加糖，如果加糖的话，要原价上加0.5元。怎么解决这样的问题？\n",
    "# 可以选择装饰器模式来解决这一类的问题。首先，定义装饰器类：\n",
    "class drinkDecorator():\n",
    "    def getName(self):\n",
    "        pass\n",
    "    def getPrice(self):\n",
    "        pass\n",
    "\n",
    "class iceDecorator(drinkDecorator):\n",
    "    def __init__(self, beverage):\n",
    "        self.beverage = beverage\n",
    "    def getName(self):\n",
    "        return self.beverage.getName() + \" +ice\"\n",
    "    def getPrice(self):\n",
    "        return self.beverage.getPrice() + 0.3\n",
    "\n",
    "class sugarDecorator(drinkDecorator):\n",
    "    def __init__(self, beverage):\n",
    "        self.beverage = beverage\n",
    "    def getName(self):\n",
    "        return self.beverage.getName() + \" +sugar\"\n",
    "    def getPrice(self):\n",
    "        return self.beverage.getPrice() + 0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:coke\n",
      "Price:4.0\n",
      "Name:coke +ice\n",
      "Price:4.3\n",
      "Name:coke +sugar\n",
      "Price:4.5\n"
     ]
    }
   ],
   "source": [
    "#构建好装饰器后，在具体的业务场景中，就可以与饮料类进行关联。以可乐+冰为例，示例业务场景如下：\n",
    "#对 实例对象 coke_cola进行装饰, 生成新的对象ice_cola, sugar_cola\n",
    "if  __name__==\"__main__\":\n",
    "    coke_cola = coke()\n",
    "    print(\"Name:%s\"%coke_cola.getName())\n",
    "    print(\"Price:%s\"%coke_cola.getPrice())\n",
    "    ice_coke =iceDecorator(coke_cola) \n",
    "    print(\"Name:%s\" % ice_coke.getName())\n",
    "    print(\"Price:%s\" % ice_coke.getPrice())\n",
    "    sugar_coke=sugarDecorator(coke_cola) \n",
    "    print(\"Name:%s\" % sugar_coke.getName())\n",
    "    print(\"Price:%s\" % sugar_coke.getPrice())      \n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
